package org.linys.util;

import org.springframework.stereotype.Component;


@Component
public class PageUtil {
	/**
	 * 当前是第几页
	 */
	private Integer page;
	/**
	 * 每页显示多少条数据
	 */
	private Integer rows;
	
	/**
	 * 计算总条数的补全sql：select count(1) from 
	 * ps : T_Operator 
	 *    : (select * from T_Operator where id = 1)a
	 */
	private String totalSql;
	/**
	 * 总共的条数
	 */
	private Integer total;
	/**
	 * 开始的条数
	 */
	private Integer start;
	/**
	 * 结束的条数
	 */
	private Integer end;
	/**
	 * 总的页码数
	 */
	private Integer totalPage;
	
	
	
	
	public PageUtil()
	{  
		
	}

	public void cal(int total,String rows,String page){
		
		try{
			this.total = total;
			if(rows==null){
				rows=10+"";
			}
			this.rows = Integer.parseInt(rows);
			this.totalPage = (this.total+this.rows-1)/this.rows;//计算总共的页码
			
			if(page==null){
				this.page=1;
			}
			
			if(page != null && !page.trim().equals("")) {
				try {
					this.page = Integer.parseInt(page);
				} catch (NumberFormatException e) {
					this.page = 1;
				} 
			}
			/*
			 * 验证所给行号的正确性
			 */
			if(this.page<=0){
				this.page=1;
			}else if(this.page>this.total){
				this.page=this.total;
			}
			this.start=(this.page-1)*this.rows;//计算开始的行号
			this.end = this.start + this.rows;
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	
	
	public Integer getPage() {
		return page;
	}


	public void setPage(Integer page) {
		this.page = page;
	}


	public Integer getRows() {
		return rows;
	}


	public void setRows(Integer rows) {
		this.rows = rows;
	}


	public String getTotalSql() {
		return totalSql;
	}


	public void setTotalSql(String totalSql) {
		this.totalSql = totalSql;
	}


	public Integer getTotal() {
		return total;
	}


	public void setTotal(Integer total) {
		this.total = total;
	}


	public Integer getStart() {
		return start;
	}


	public void setStart(Integer start) {
		this.start = start;
	}


	public Integer getEnd() {
		return end;
	}


	public void setEnd(Integer end) {
		this.end = end;
	}




	public Integer getTotalPage() {
		return totalPage;
	}


	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

}
